/**
 *    Copyright 2009-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.learning.type;

import lombok.SneakyThrows;
import org.apache.ibatis.reflection.TypeParameterResolver;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class TypeShower {

    @Test
    @SneakyThrows
    public void assertType() {
        // 获取getEntities方法
        Method getEntitiesMethod = Sub.class.getMethod("getEntities");
        assert Super.class.equals(getEntitiesMethod.getDeclaringClass());

        // 获取方法返回值的泛型定义
        Type returnType = getEntitiesMethod.getGenericReturnType();
        assert returnType instanceof ParameterizedType;

        // 获取父接口的定义
        Type superType = Sub.class.getGenericInterfaces()[0];
        assert superType instanceof ParameterizedType;
        assert ((ParameterizedType) superType).getActualTypeArguments()[1].equals(String.class);
    }

    @Test
    @SneakyThrows
    public void assertType2() {
        // 获取getEntities方法
        Method selectMethod = GrandSon.class.getMethod("select");
        TypeParameterResolver.resolveReturnType(selectMethod, GrandSon.class);
    }

    private interface Super<E, F> {
        List<E> getEntities();

        E select();
    }

    private interface Sub<E> extends Super<E, String> {
    }

    private interface GrandSon<D> extends Sub<D>{}
}
